package com.zjw.runner;

import com.zjw.constants.SystemConstants;
import com.zjw.annotation.domain.entity.Article;
import com.zjw.mapper.ArticleMapper;
import com.zjw.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class TestRunner implements CommandLineRunner {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RedisCache redisCache;

    @Override
    public void run(String... args) throws Exception {
//        获取所有的id和对应viewCount通过hash的方式存在Redis中
        List<Article> list = articleMapper.selectList(null);
        Map<String, Integer> map = list.stream().collect(Collectors.toMap(
                (e) -> e.getId().toString(),
                (e) -> e.getViewCount().intValue()
        ));
        redisCache.setCacheMap(SystemConstants.REDIS_VIEW_COUNT_KEY, map);
    }
}
